package com.business.etl.flow;

import com.google.common.base.Preconditions;
import com.google.common.graph.Graphs;
import com.google.common.graph.MutableValueGraph;
import com.google.common.graph.ValueGraphBuilder;
import org.apache.commons.lang3.StringUtils;

/**
 * 描述：
 *
 * @author xianggj
 * @Date 2021/10/15 15:39
 **/
public class LearnGraphs {

    public static void main(String[] args) {
        test();
    }

    static void test() {

        /**
         * <pre>
         * Graph
         *           a  -> c
         *              -> b -> d -> e
         * </pre>
         */
        MutableValueGraph<Node<DataEntity>, Edge<DataRelationShip>> graph =
                ValueGraphBuilder.directed().build();
        String nameSpace = "nameSpace";
        Node<DataEntity> a = new DataEntity(nameSpace, "a");
        Node<DataEntity> b = new DataEntity(nameSpace, "b");
        Node<DataEntity> c = new DataEntity(nameSpace, "c");
        Node<DataEntity> d = new DataEntity(nameSpace, "d");
        Node<DataEntity> e = new DataEntity(nameSpace, "e");

        Node<DataEntity> eClone = new DataEntity(nameSpace, "e");

        Edge<DataRelationShip> ab = new DataRelationShip("a", "b");
        Edge<DataRelationShip> ac = new DataRelationShip("a", "c");
        Edge<DataRelationShip> bd = new DataRelationShip("b", "d");
        Edge<DataRelationShip> de = new DataRelationShip("d", "e");
        // Edge<DataRelationShip> ea = new DataRelationShip("e", "a");

        putEdgeValue(graph, a, b, ab);
        putEdgeValue(graph, a, c, ac);
        putEdgeValue(graph, b, d, bd);
        putEdgeValue(graph, d, e, de);
        // putEdgeValue(graph, e, a, ea);
        System.out.println(graph.toString());
        System.out.println(Graphs.hasCycle(graph.asGraph()));
        System.out.println(graph.adjacentNodes(b));
        System.out.println(StringUtils.repeat("-", 100));
        System.out.println(graph.predecessors(a));
        System.out.println(graph.successors(a));
        System.out.println(StringUtils.repeat("-", 100));
        System.out.println(graph.predecessors(b));
        System.out.println(graph.successors(b));
        System.out.println(StringUtils.repeat("-", 100));
        System.out.println(graph.predecessors(eClone));
        System.out.println(graph.successors(eClone));

    }

    static void putEdgeValue(MutableValueGraph<Node<DataEntity>, Edge<DataRelationShip>> graph,
                             Node<DataEntity> a, Node<DataEntity> b, Edge<DataRelationShip> ab) {
        graph.putEdgeValue(a, b, ab);
        Preconditions.checkArgument(!Graphs.hasCycle(graph.asGraph()),
                "from node:%s to node:%s with the edge:%s will form the cycle.", a, b, ab);
    }
}
